package com.example.cuibo.myapplicationhttp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.IntStream;

public class MainActivity extends AppCompatActivity {

    private EditText text;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);
        // 找到控件
         text = (EditText)findViewById(R.id.ed_path);

        textView = (TextView)findViewById(R.id.tv_result);
    }
    public void click(){
        try {
            // 获取源码路径
            String path =  text.getText().toString().trim();
            // 创建url对象
            URL rul = new URL(path);
            // 拿到httpurlconection对象 用于发送接收数据
            HttpURLConnection coneciton =  (HttpURLConnection) rul.openConnection();
            // 设置发送get 请求
            coneciton.setRequestMethod("GET"); // 大写  默认GET
            // 设置超时时间
            coneciton.setConnectTimeout(5000);
            // 获取状态码
            int code = coneciton.getResponseCode();
            // 对请求判断
            if (code == 200){
                // 获取服务器的数据 是以流的形似返回的
                // 把流转换成字符串
                InputStream in = coneciton.getInputStream();
                String content = StreamTools.redStream(in);
                textView.setText(content);
            }
        }  catch (Exception e){

        }


    }
}
